package com.yz.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yz.common.annotation.Login;
import com.yz.common.annotation.LoginUser;
import com.yz.common.entity.*;
import com.yz.common.utils.R;
import com.yz.common.validator.ValidatorUtils;
import com.yz.form.GetListForm;
import com.yz.form.GetSearchListForm;
import com.yz.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.*;

/**
 * 图形资料相关操作
 *
 * @author yz

 *
 */
@RestController
@RequestMapping("/api")
@Api(tags="图形资料")
public class ApiPictureController {
    @Autowired
    private DrawingService drawingService;
    @Autowired
    private CameraService cameraService;
    @Autowired
    private VedioService vedioService;
    @Autowired
    private CheckService checkService;

    @GetMapping("getVedioDetail")
    @ApiOperation("获取视频详情")
    @Login
    public R getVedioDetail(@RequestHeader("token")String token,@RequestParam("id") long id){
        VedioEntity data = vedioService.selectOne(new EntityWrapper<VedioEntity>().eq("id",id));
        return R.ok().put("data",data);
    }

    @GetMapping("getCameraDetail")
    @ApiOperation("获取拍照详情")
    @Login
    public R getCameraDetail(@RequestHeader("token")String token,@RequestParam("id") long id){
        CameraEntity data = cameraService.selectOne(new EntityWrapper<CameraEntity>().eq("id",id));
        return R.ok().put("data",data);
    }

    @GetMapping("getDrawingDetail")
    @ApiOperation("获取绘图详情")
    @Login
    public R getDrawingDetail(@RequestHeader("token")String token,@RequestParam("id") long id){
        DrawingEntity data = drawingService.selectOne(new EntityWrapper<DrawingEntity>().eq("id",id));
        return R.ok().put("data",data);
    }

    @GetMapping("getPictureList")
    @ApiOperation("获取图形资料列表")
    @Login
    public R getPictureList(@RequestHeader("token")String token,@RequestParam(value = "order") int order,@RequestParam(value = "keyWord",required = false) String keyWord,@RequestParam(value = "type_where" ) int type_where,@RequestParam("page") int page,@RequestParam("pageSize") int pageSize,@RequestParam("projectId") long projectId,@ApiIgnore @LoginUser UserEntity user){
        if(keyWord==null){
           keyWord="";
        }
        Map<String ,Object> map1 = new HashMap<>();
        map1.put("userid",user.getUserId());
        map1.put("keyWord","%"+keyWord+"%");
        map1.put("typeWhere",type_where);
        map1.put("projectId",projectId);
        int total =drawingService.selectCountP(map1);
        int from = page*pageSize;
        int to =(page+1)*pageSize;
        if((page+1)*pageSize>=total){
            to=total;
        }
        map1.put("groupe",order);
        map1.put("from",from);
        map1.put("to",to);
        List<Map<String,Object>> data = drawingService.selectPictures(map1);
//        int total = checkService.selectCount(new EntityWrapper<CheckEntity>().like("number",keyWord).like("name_form",keyWord).eq("type_form",type_where).where("type=2 or type=3 or type=4").eq("project_id",projectId).eq("create_user",user.getUsername()));
//        Page pages = checkService.selectPage(new Page<CheckEntity>(page+1,pageSize),new EntityWrapper<CheckEntity>().like("number",keyWord).like("name_form",keyWord).eq("type_form",type_where).where("type=2 or type=3 or type=4").eq("project_id",projectId).eq("create_user",user.getUsername()).orderBy("create_at desc,id desc"));
        return R.ok().put("data",data).put("total",total);
    }

    @GetMapping("getVedioList")
    @ApiOperation("获取视频列表")
    @Login
    public R getVedioList(@RequestHeader("token")String token,@RequestParam(value = "type_where" ) int type_where,@RequestParam("page") int page,@RequestParam("pageSize") int pageSize,@RequestParam("projectId") long projectId, @ApiIgnore @LoginUser UserEntity user){
        int total = vedioService.selectCount(new EntityWrapper<VedioEntity>().eq("type_where",type_where).eq("poster_id",user.getUserId()).eq("project_id",projectId));
        Page pages = vedioService.selectPage(new Page<VedioEntity>(page+1,pageSize),new EntityWrapper<VedioEntity>().eq("type_where",type_where).eq("poster_id",user.getUserId()).eq("project_id",projectId).orderBy("post_time desc,id desc"));
        return R.ok().put("data",pages.getRecords()).put("total",total);
    }

    @GetMapping("getCameraList")
    @ApiOperation("获取拍照列表")
    @Login
    public R getCameraList(@RequestHeader("token")String token,@RequestParam(value = "type_where" ) int type_where,@RequestParam("page") int page,@RequestParam("pageSize") int pageSize,@RequestParam("projectId") long projectId, @ApiIgnore @LoginUser UserEntity user){
        R r = new R();
        int total = cameraService.selectCount(new EntityWrapper<CameraEntity>().eq("type_where",type_where).eq("poster_id",user.getUserId()).eq("project_id",projectId));
        Page pages = cameraService.selectPage(new Page<CameraEntity>(page+1,pageSize),new EntityWrapper<CameraEntity>().eq("type_where",type_where).eq("poster_id",user.getUserId()).eq("project_id",projectId).orderBy("post_time desc,id desc"));
        r.put("data",pages.getRecords());
        r.put("code",0);
        return R.ok().put("data",pages.getRecords()).put("total",total);
    }

    @GetMapping("getDrawingList")
    @ApiOperation("获取绘图列表")
    @Login
    public R getDrawingList(@RequestHeader("token")String token,@RequestParam(value = "type_where" ) int type_where, @RequestParam("page") int page,@RequestParam("pageSize") int pageSize,@RequestParam("projectId") long projectId, @ApiIgnore @LoginUser UserEntity user){
        R r = new R();
        int total =drawingService.selectCount(new EntityWrapper<DrawingEntity>().eq("type_where",type_where).eq("poster_id",user.getUserId()).eq("project_id",projectId));
        Page pages = drawingService.selectPage(new Page<DrawingEntity>(page+1,pageSize),new EntityWrapper<DrawingEntity>().eq("type_where",type_where).eq("project_id",projectId).eq("poster_id",user.getUserId()).orderBy("post_time desc,id desc"));
        return R.ok().put("data",pages.getRecords()).put("total",total);
    }

    @DeleteMapping("deleteVedio")
    @ApiOperation("删除视频")
    @Login
    public R deleteVedio(@RequestHeader("token")String token,@RequestParam("id") long id){
        try {
            vedioService.deleteMe(id);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @DeleteMapping("deleteCamera")
    @ApiOperation("删除照片")
    @Login
    public R deleteCamera(@RequestHeader("token")String token,@RequestParam("id") long id){
        try {
            cameraService.deleteMe(id);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @DeleteMapping("deleteDrawing")
    @ApiOperation("删除绘图")
    @Login
    public R deleteDrawing(@RequestHeader("token")String token,@RequestParam("id") long id){
        try {
            drawingService.deleteMe(id);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @PostMapping("postDrawing")
    @ApiOperation("提交或修改绘图")
    @Login
    public R postDrawing(@RequestHeader("token")String token,@RequestBody DrawingEntity t, @ApiIgnore @LoginUser UserEntity user){
        ValidatorUtils.validateEntity(t);
        try {
            drawingService.postMe(t);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @PostMapping("postCamera")
    @ApiOperation("提交或修改拍照")
    @Login
    public R postCamera(@RequestHeader("token")String token,@RequestBody CameraEntity t, @ApiIgnore @LoginUser UserEntity user){
        ValidatorUtils.validateEntity(t);
        try {
            cameraService.postMe(t);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }

    @PostMapping("postVedio")
    @ApiOperation("提交或修改视频")
    @Login
    public R postVedio(@RequestHeader("token")String token,@RequestBody VedioEntity t, @ApiIgnore @LoginUser UserEntity user){
        ValidatorUtils.validateEntity(t);
        try {
            vedioService.postMe(t);
            return R.ok().put("data",null);
        }catch (Exception e){
            return R.error().put("data",null);
        }
    }
}
